package com.xxl.filter;

import lombok.extern.slf4j.Slf4j;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 字符过滤器
 * @author xxl
 * @date 2023/3/29
 */
@Order(520)
@Slf4j
@WebFilter(filterName = "EncodingFilter",urlPatterns = "/*",description = "字符过滤器")
public class EncodingFilter  implements Filter {
    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        log.error("使用了字符过滤器");
        HttpServletRequest rep = (HttpServletRequest) request;
        HttpServletResponse resp = (HttpServletResponse) response;
        //设置请求，响应 的字符集都为utf-8
        rep.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");
        //设置响应头的类型
        resp.setContentType("application/json;charset=utf-8");
        chain.doFilter(request,response);
    }
}
